<template>
<div class="login-box">
  <nav-bar title="登录" :isBack="false" />
  <div class="login-tab">
    <span class="flex active bd-r">登录</span>
    <span class="flex" @click="goReg">注册</span>
  </div>
  <div class="content">
    <div class="form">
      <j-input type="text" v-model="mobileValue" label="手机号" max="11" class="bd-b" placeholder="请输入手机号"/>
      <j-input type="password" v-model="passwordValue" label="密码" placeholder="请输入登录密码"/>
    </div>
    <j-botton type="danger" class="loginBtn" :disabled="disabled" :loading="isLoading" loadingText='登录中' @click.native="login" style="margin-top:2.78rem;">登录</j-botton>
    <div class="Forgot-password">
      <span @click="goForgotPassword">忘记密码？</span>
    </div>
  </div>
</div>
</template>
<script>
import md5 from 'md5';
export default {
  name: 'login',
  data() {
    return {
      mobileValue: '',
      isLoading: false,
      disabled: false,
      passwordValue: '' // 9db06bcff9248837f86d1a6bcf41c9e7
    };
  },
  created() {
  },
  mounted() {
    console.log(location.href);
  },
  methods: {
    login() {
      if (this.isLoading) {
        return;
      }
      var disabled = !(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.mobileValue));
      if (disabled) {
        this.$toast('请输入正确的手机号!');
        return;
      }
      var _account = this.mobileValue + '';
      var _password = this.passwordValue + '';
      if (!_account) {
        this.$toast('手机号不能为空');
        return;
      }
      if (!_password) {
        this.$toast('密码不能为空');
        return;
      }
      var options = {
        mobile: this.mobileValue,
        password: md5(md5(this.passwordValue))
      };
      this.isLoading = true;
      this.disabled = true;
      this.$API.user.login(options).then(res => {
        if (res.code === 0) {
          localStorage.setItem('token', res.data);
          this.$router.push({ path: '/home' });
        }
        this.isLoading = false;
        this.disabled = false;
      });
    },
    goForgotPassword() {
      this.$router.push({ path: '/forgotPassword' });
    },
    goReg() {
      this.$router.push({ path: '/register' });
    }
  }
};
</script>
